//
//  MarketManageTableViewCell.swift
//  kxf
//
//  Created by Mac on 2024/3/10.
//

import Foundation
import UIKit

class MarketManageTableViewCell: UITableViewCell {
    
    var changeBlock:(()->())?
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .clear
        self.selectionStyle = .none
        initUI()
    }
    lazy var bgView : UIView = {
        let vi = UIView()
        vi.backgroundColor = WHColorWithAlpha(colorStr: "F10E0E", alpha: 0.08)
        vi.layer.cornerRadius = kFitWidth(8)
        vi.clipsToBounds = true
        vi.isUserInteractionEnabled = true
        
        return vi
    }()
    lazy var iconImgView : UIImageView = {
        let img = UIImageView()
        img.setImgLocal(imgName: "market_mange_icon")
        
        return img
    }()
    lazy var typeLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 14, weight: .medium)
        lab.textAlignment = .center
        lab.adjustsFontSizeToFitWidth = true
        
        return lab
    }()
    lazy var mercNoLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 16, weight: .medium)
        lab.adjustsFontSizeToFitWidth = true
        
        return lab
    }()
    lazy var timeLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = .COLOR_TEXT_BLACK666
        lab.font = .systemFont(ofSize: 14, weight: .regular)
        lab.adjustsFontSizeToFitWidth = true
        
        return lab
    }()
    lazy var statusLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = .THEME
        lab.font = .systemFont(ofSize: 14, weight: .regular)
//        lab.text =
        
        return lab
    }()
    lazy var changeImgView : UIImageView = {
        let img = UIImageView()
        img.setImgLocal(imgName: "market_mange_change")
        img.isUserInteractionEnabled = true
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(changeAction))
        img.addGestureRecognizer(tap)
        
        return img
    }()
}

extension MarketManageTableViewCell{
    func updateUI(dict:NSDictionary) {
        
        mercNoLabel.text = "店铺编号：\(dict["username"]as? String ?? "")"
        timeLabel.text = "申请时间：\(Date().timestampToDate(timestamp: TimeInterval((dict["createTime"]as? Int ?? 0)/1000)))"
        
        //类型
        if dict["productRecommendAge"]as? String ?? "" == "0"{
            typeLabel.text = "企业"
        }else if dict["productRecommendAge"]as? String ?? "" == "2"{
            typeLabel.text = "个体"
        }else{
            typeLabel.text = "小微"
        }
        //状态
        if dict["posStatus"]as? Int ?? -5 == -2 || dict["posStatus"]as? Int ?? -5 == -1{
            statusLabel.text = "认证状态：已驳回"
        }else if dict["posStatus"]as? Int ?? -5 == 0{
            statusLabel.text = "认证状态：待提交"
        }else if dict["posStatus"]as? Int ?? -5 == 1{
            statusLabel.text = "认证状态：审核中"
        }else if dict["posStatus"]as? Int ?? -5 == 2{
            statusLabel.text = "认证状态：待授权"
        }else if dict["posStatus"]as? Int ?? -5 == 3{
            statusLabel.text = "认证状态：已绑定"
        }else if dict["posStatus"]as? Int ?? -5 == 4{
            statusLabel.text = "认证状态：已认证"
        }
        if "\(dict["id"]as? Int ?? 0)" != UserModel.shared.memberId{
            changeImgView.isHidden = false
        }else{
            changeImgView.isHidden = true
        }
    }
    
    @objc func changeAction() {
        if self.changeBlock != nil{
            self.changeBlock!()
        }
    }
}
extension MarketManageTableViewCell{
    func initUI(){
        contentView.addSubview(bgView)
        bgView.addSubview(iconImgView)
        bgView.addSubview(typeLabel)
        bgView.addSubview(mercNoLabel)
        bgView.addSubview(timeLabel)
        bgView.addSubview(statusLabel)
        bgView.addSubview(changeImgView)
        
        setConstrait()
    }
    func setConstrait(){
        bgView.snp.makeConstraints { make in
            make.top.equalTo(kFitWidth(10))
            make.centerX.lessThanOrEqualToSuperview()
            make.width.equalTo(kFitWidth(343))
            make.height.equalTo(kFitWidth(100))
        }
        iconImgView.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(10))
            make.top.equalTo(kFitWidth(18))
            make.width.height.equalTo(kFitWidth(44))
        }
        typeLabel.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(2))
            make.width.equalTo(kFitWidth(56))
            make.top.equalTo(iconImgView.snp.bottom).offset(kFitWidth(10))
        }
        mercNoLabel.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(60))
            make.top.equalTo(kFitWidth(17))
            make.right.equalTo(kFitWidth(-10))
        }
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(mercNoLabel)
            make.top.equalTo(kFitWidth(43))
            make.right.equalTo(kFitWidth(-65))
        }
        statusLabel.snp.makeConstraints { make in
            make.left.equalTo(mercNoLabel)
            make.top.equalTo(kFitWidth(72))
        }
        changeImgView.snp.makeConstraints { make in
            make.right.equalTo(kFitWidth(-10))
            make.centerY.lessThanOrEqualToSuperview()
            make.width.height.equalTo(kFitWidth(34))
        }
    }
}
